/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"E43F27F4-AB26-412C-966C-DB7E372433CF",variableType:4}
 */
var fCodSub = null;

/**
 * @type {String}
 *
 * @properties={typeid:35,uuid:"CC3CCC68-0701-4578-8577-5AB7F37770F6"}
 */
var fDescSub = null;

/**
 * Callback method for when form is shown.
 *
 * @param {Boolean} [firstShow] form is shown first time after load
 * @param {JSEvent} [event] the event that triggered the action
 *
 * @properties={typeid:24,uuid:"C5B4E992-D6D8-4B84-9CC7-C678363555C5"}
 */
function onShow_alta(firstShow, event) {
	globals.gvarios 		= 5
	globals.gcapitulo 		= 0
	globals.gsubcapitulo 	= 0
	globals.grubro 			= 0
	databaseManager.setAutoSave(false)
	controller.newRecord()
	marca_sub = 0
	marca_imprimir = 0
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"2F8B1866-5DCF-4D20-B5E6-A2D6E82E8619"}
 * @AllowToRunInFind
 */
function onAction_alta(event) {
	if (cuenta == null || cuenta == 0)
	{
		globals.DIALOGS.showWarningDialog("Aviso",'Debe definir la cuenta','OK')
		return
	}
	if (capitulo == null || capitulo == 0)
	{
		globals.DIALOGS.showWarningDialog("Aviso",'Debe definir el capítulo','OK')
		return
	}
	if (subcapitulo == null || subcapitulo == 0)
	{
		globals.DIALOGS.showWarningDialog("Aviso",'Debe definir el subcapítulo','OK')
		return
	}
	if (rubro == null || rubro == 0)
	{
		globals.DIALOGS.showWarningDialog("Aviso",'Debe definir el rubro','OK')
		return
	}
	if (descripcion == null)
	{
		globals.DIALOGS.showWarningDialog("Aviso",'Debe definir la descripcion','OK')
		return
	}
	if (marca_mon == null || marca_mon == 0)
	{
		globals.DIALOGS.showWarningDialog("Aviso",'Debe definir el tipo de cuenta','OK')
		return
	}
	if (marca_sub == null)
	{
		globals.DIALOGS.showWarningDialog("Aviso",'Debe definir el si tiene subcuentas','OK')
		return
	}
	// verifica si la cuenta ya existe
	var $sql = 'SELECT '+ 
					'descripcion '+
				'FROM cuentas '+
				'WHERE '+
					'capitulo = ? AND '+
					'subcapitulo = ? AND '+
					'rubro = ? AND '+
					'cuenta = ? AND '+
					'subcuenta = ?'
	var $args = [capitulo, subcapitulo, rubro, cuenta, 0]
	var $ds = databaseManager.getDataSetByQuery('contable',$sql,$args,1)
	if ($ds.getMaxRowIndex() > 0)
	{
		globals.DIALOGS.showWarningDialog("Aviso","La cuenta " + $ds.getValue(1,1) + " ya existe - Verifique ","Ok")
		return
	}
	if (marca_sub == 0)
	{
		imputable = 1	
	}
	else
	{
		imputable = 0		
	}
	activa 		= 0
	subcuenta 	= 0
	databaseManager.startTransaction()
	if (!databaseManager.saveData())
	{
		databaseManager.rollbackTransaction()
		databaseManager.revertEditedRecords()
		globals.DIALOGS.showWarningDialog('Aviso','No pudo grabar','OK')
		
	}
	else
	{
		databaseManager.commitTransaction()
		globals.DIALOGS.showInfoDialog('Aviso','Cuenta Dada de Alta','OK')
		onShow_alta()
	}
}

/**
 * Handle changed data.
 *
 * @param {Object} oldValue old value
 * @param {Object} newValue new value
 * @param {JSEvent} event the event that triggered the action
 *
 * @returns {Boolean}
 *
 * @properties={typeid:24,uuid:"A8CF0517-80D6-4751-986D-F7837D2F2BE9"}
 */
function onDataChange_capitulo(oldValue, newValue, event) {
	globals.gcapitulo = capitulo
	return true
}

/**
 * Handle changed data.
 *
 * @param {Object} oldValue old value
 * @param {Object} newValue new value
 * @param {JSEvent} event the event that triggered the action
 *
 * @returns {Boolean}
 *
 * @properties={typeid:24,uuid:"332227BE-F17B-40D4-8100-FB5F7F73B97D"}
 */
function onDataChange_subcapitulo(oldValue, newValue, event) {
	globals.gsubcapitulo = subcapitulo
	return true
}

/**
 * Handle changed data.
 *
 * @param {Object} oldValue old value
 * @param {Object} newValue new value
 * @param {JSEvent} event the event that triggered the action
 *
 * @returns {Boolean}
 *
 * @properties={typeid:24,uuid:"7578A05C-ACFB-4270-9C37-8BB71D12A63F"}
 */
function onDataChange_rubros(oldValue, newValue, event) {
	globals.grubro = rubro	
	return true
}

/**
 * Handle changed data.
 *
 * @param {Object} oldValue old value
 * @param {Object} newValue new value
 * @param {JSEvent} event the event that triggered the action
 *
 * @returns {Boolean}
 *
 * @properties={typeid:24,uuid:"0985904A-238E-4657-BA93-83FA63D65422"}
 * @AllowToRunInFind
 */
function onDataChange_cuenta(oldValue, newValue, event) {
	if (capitulo == null || subcapitulo == null || rubro == null)
	{
		globals.DIALOGS.showWarningDialog("Aviso","Faltan datos para verificar si la cuenta existe","Ok")
		return true	
	}
	/** @type {JSFoundSet<db:/contable/cuentas>}*/
    var fs_cuentas = databaseManager.getFoundSet("contable","cuentas")
    var estado = fs_cuentas.find()
    	if (estado == false)
	{
		globals.DIALOGS.showWarningDialog("Aviso","Error al hacer el find","Ok")
		return true
	}
    fs_cuentas.capitulo = capitulo
    fs_cuentas.subcapitulo = subcapitulo
    fs_cuentas.rubro = rubro
    fs_cuentas.cuenta = cuenta
    fs_cuentas.subcuenta = 0
    var cuantos = fs_cuentas.search()
	if (cuantos != 0)
	{
		descripcion = fs_cuentas.descripcion
		globals.DIALOGS.showWarningDialog("Aviso","La cuenta ya existe - Verifique ","Ok")
		descripcion = " "
		return false		
	}
	else
		return true
}

/**
 * Handle hide window.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @returns {Boolean}
 *
 * @properties={typeid:24,uuid:"DA88BB32-9689-4C72-B245-FDDAA7CA9BF8"}
 */
function onHide(event) {
	databaseManager.revertEditedRecords()
	databaseManager.setAutoSave(true)
	return true
}
